package com.jader.activities;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonArrayRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jader.adapter.MerchantAdapter;
import com.jader.beans.Goods;
import com.jader.myviews.SearchClearEditText;
import com.jader.util.JaderApplication;
import com.zlp.jader.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;

public class SouDetailActivity extends Activity {
	// 从web端获取数据 将其设置到对应的adapter

	private RequestQueue queue;
	private String url = JaderApplication.getIP() + "/Jader/client?type="
			+ JaderApplication.SEARCH_GOODS + "&goodsname=";

	private MerchantAdapter adapter;
	private List<Goods> list = new ArrayList<Goods>();
	private XListView xListView;
	int page;
	SearchClearEditText editText;
	ImageButton button;
	private Request<?> request;
	private String result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sou_detail);
		xListView = (XListView) findViewById(R.id.sou_detail);
		editText = (SearchClearEditText) findViewById(R.id.et_clear);
		button = (ImageButton) findViewById(R.id.ib_back_normal);
		JaderApplication myApplication = (JaderApplication) getApplication();
		queue = myApplication.getQueue();
		// 获取数据库数据 设置样式
		adapter = new MerchantAdapter(list, this);
		xListView.setAdapter(adapter);
		result = getIntent().getStringExtra("goodsname");
		page = 0;
		sendRequest(url + result + "&page=" + page);
		xListView.setPullLoadEnable(true);
		xListView.setXListViewListener(new IXListViewListener() {
			// 刷新
			@Override
			public void onRefresh() {
				page = 0;
				sendRequest(url + result + "&page=" + page);
			}

			// 加载
			@Override
			public void onLoadMore() {
				page = list.size();
				sendRequest(url + result + "&page=" + page);
			}
		});
		xListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// 跳转到搜索商品信息界面 并能进行购买
				Intent intent = new Intent(SouDetailActivity.this, Seller.class);
				intent.putExtra("goods", list.get(arg2 - 1));
				startActivity(intent);
			}
		});
	}

	@SuppressWarnings("unused")
	private void onLoad() {
		xListView.stopRefresh();
		xListView.stopLoadMore();
		xListView.setRefreshTime("刚刚");
	}

	// 搜索显示界面的单击编辑框 返回到搜索界面
	public void backsou(View view) {
		Intent intent = new Intent(SouDetailActivity.this, SouSuoActivity.class);
		startActivity(intent);
	}

	public void back(View view) {
		Intent intent = new Intent(SouDetailActivity.this, SouSuoActivity.class);
		startActivity(intent);
	}

	private void sendRequest(final String url) {
		// 发送请求
		request = new JsonArrayRequest(url, new Listener<JSONArray>() {
			@Override
			public void onResponse(JSONArray response) {
				Gson gson = new Gson();
				Type type = new TypeToken<ArrayList<Goods>>() {
				}.getType();
				List<Goods> list1 = gson.fromJson(response.toString(), type);
				for (Goods goods : list1) {
					boolean flag = false;
					for (Goods good2 : list) {
						if (goods.equals(good2)) {
							flag = true;
						}
					}
					if (!flag) {

						list.add(page, goods);
						page++;

					}
				}
				adapter.notifyDataSetChanged();
			}

		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {

			}
		});
		queue.add(request);

	}
}
